package com.pig4cloud.pigx.auth.handler;

import com.pig4cloud.pigx.common.security.handler.AuthenticationFailureHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @Author 兔子不吃窝边曹
 * @create 2022/8/5 9:54
 **/
@Slf4j
@Component
@AllArgsConstructor
public class PigxAuthenticationFailureLogEventHandler implements AuthenticationFailureHandler {

    /**
     * 登录失败方法
     *
     * @param authenticationException 错误信息
     * @param authentication          认证信息
     * @param request                 请求信息
     * @param response                响应信息
     */
    @Async
    @Override
    public void handle(AuthenticationException authenticationException, Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
        String username = authentication.getName();

        // 可以进行日志入库

        log.info("用户：{} 登录失败，异常：{}", username, authenticationException.getLocalizedMessage());
    }
}
